﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileStreamsAndCrypto
{
    class BinaryStorage
    {
        const string STORAGE_FILE = @"C:\Users\Academy\Desktop\file.bin";
        private const int BUFFER_SIZE = 1024;

        static public void WriteToFile(Student s, string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Append);
            BinaryWriter br = new BinaryWriter(fs);
            using (br)
            {
                br.Write(s.FirstName);
                br.Write(s.LastName);
                br.Write(s.FNumber);
            }
        }

        static public void DeleteFromFile(string fileName, int fNumber)
        {
            BinaryReader br = new BinaryReader(File.OpenRead(fileName));
            string file = Path.GetTempFileName();
            BinaryWriter bw = new BinaryWriter(File.OpenWrite(file));
            using (br)
            {
                using (bw)
                {
                    while(br.PeekChar() != -1)
                    {
                        string fName = br.ReadString();
                        string lName = br.ReadString();
                        int fNum = br.ReadInt32();
                        if (fNumber != fNum)
                        {
                            bw.Write(fName);
                            bw.Write(lName);
                            bw.Write(fNum);
                        }
                    }
                }
            }
            File.Delete(fileName);
            File.Move(file, fileName);
        }

        //static void Main()
        //{
        //    const string WRITE_FILE = @"C:\Users\Academy\Desktop\students.bin";

        //    Student s = new Student("Filip", "Slanchev", 71057);
        //    Console.WriteLine(s);
        //    WriteToFile(s, WRITE_FILE);

        //    Student s1 = new Student("Svetlin", "Nakov", 71127);
        //    WriteToFile(s1, WRITE_FILE);

        //    DeleteFromFile(WRITE_FILE, 71127);
        //}
    }
}
